package com.gopay.bis.web.controller.expay;

import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.BisBankInterfaceChannel;
import com.gopay.common.constants.bank.YspayAdpayConstants;
import com.gopay.common.domain.bank.ExPayBaseMessage;
import com.gopay.common.domain.bank.yspay.expay.pay.YspayAdPayMessage;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
@Scope("prototype")
public class YspayAdPayServerRes extends ExPayBankResBase {
    @Resource(name = "yspayConfig")
    private Map<String, String> yspayConfig;
    private static Log logger = LogFactory.getLog(YspayAdPayServerRes.class);

    @RequestMapping(value = "/yspayAdPayServerRes")
    protected ModelAndView execute(HttpServletRequest request,
                                   HttpServletResponse response) {
        logger.error("--------------- yspayAdPayServerRes bankback  ---------------");
        return super.execute(request, response);
    }

    @Override
    protected ExPayBaseMessage buildMessage(HttpServletRequest request) {
        YspayAdPayMessage message = new YspayAdPayMessage();
        message.setBank(BankConstants.BANK_LOGO_YSPAY);

        message.setBankBusProd(BisBankBusinessProd._15);
        // 银行接口通道信息 12: 银盛
        message.setBankIntrChann(BisBankInterfaceChannel._12);
        message.setBankUrl(yspayConfig.get("YSPAY.AD.BANKURL"));
        message.setBankPost(request.getParameter("msg"));
        message.setSrc(request.getParameter("src"));
        message.setMsgCode(request.getParameter("msgCode"));
        message.setMsg(request.getParameter("msg"));
        message.setCheck(request.getParameter("check"));
        message.setSignMessage(request.getParameter("check"));
        logger.info("--------------- yspayAdPayServerRes buildMessage  ---------------");
        logger.info("--------------- request.getParameter  ---------------"+message.getBankPost());
        return message;
    }

    @Override
    protected OrderInfo buildOrderInfo(HttpServletRequest request, Result result) {
        YspayAdPayMessage message = (YspayAdPayMessage) result
                .getModel("res");
        logger.error("yspayAdPayServerRes notify:"
                + ToStringBuilder.reflectionToString(message));
        OrderInfo orderInfo = new OrderInfo();
        if (message != null) {
            String state = message.getBusiState(); // 订单状态
            String amt = message.getAmount().toString();
            if (YspayAdpayConstants.YSPAY_STATE_SUCCESS
                    .equals(state)) {
                orderInfo.setPayStatus("00");
                orderInfo.setRetCode("00");
                orderInfo.setRetMsg("");
                orderInfo.setIsNotify(true);
               logger.error("yspayadpay orderId:"+message.getOrderId()+"   state:"+state);
            } else{
                orderInfo.setPayStatus("01");
                orderInfo.setRetCode("01");
                orderInfo.setRetMsg(message.getNote());
                orderInfo.setIsNotify(false);
            }
            orderInfo.setOrderId(message.getOrderId());// 为了获取系统订单号
            orderInfo.setAmt(amt);
            orderInfo.setNotifyNum("1");
        }
        orderInfo.setCardType("");// 信用卡类型
        orderInfo.setBankName("yspay");
        return orderInfo;
    }

    @Override
    protected boolean checkParameters(HttpServletRequest request) {
    	if (StringUtils.isEmpty(request.getParameter("src"))
                || StringUtils.isEmpty(request.getParameter("msgId"))
                || StringUtils.isEmpty(request.getParameter("msg"))
                || StringUtils.isEmpty(request.getParameter("check"))
                || StringUtils.isEmpty(request.getParameter("msgCode"))) {
        return false;
        }
        return true;
    }

}
